package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjWeekdayConfiguration;
import com.koron.css2.newProjectCenter.bean.PjWeekdayMonth;
import com.koron.css2.newProjectCenter.mapper.PjWeekdayConfigurationMapper;
import com.koron.css2.newProjectCenter.mapper.PjWeekdayMonthMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ；lwc
 * @Date: 2023/11/07/10:43
 * @Description:
 */
public class PjWeekdayConfigurationList implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {

            MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", List.class);
            PjWeekdayConfigurationMapper mapper = factory.getMapper(PjWeekdayConfigurationMapper.class);
            PjWeekdayMonthMapper monthMapper = factory.getMapper(PjWeekdayMonthMapper.class);
            List<PjWeekdayConfiguration> list = mapper.selectAll();
            if (Objects.isNull(list)) {
                return info;
            }
            list.forEach(x -> {
                String id = x.getId();
                List<PjWeekdayMonth> monthList = monthMapper.getListByConfigurationId(id);
                List<Integer> months = monthList.stream().map(PjWeekdayMonth::getMonth).collect(Collectors.toList());
                x.setMonths(months);
            });


            info.setData(list);
            return info;
        } catch (Exception e) {
            logger.error("报装工作日配置查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "报装工作日配置查询失败", null);
        }
    }

}
